package com.resou.reader.bookstore.freezone.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.resou.reader.R;
import com.resou.reader.api.entry.FreezoneBean;
import com.resou.reader.base.ResouBaseFragment;
import com.resou.reader.bookstore.freezone.FreezoneActivity;
import com.resou.reader.bookstore.freezone.adapter.FreezoneAdapter;
import com.resou.reader.utils.ToastUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 女生频道
 */
public class GirlFragment extends ResouBaseFragment<FragmentFreezonPresenter> implements IFreezonContract.IFreenzonView, OnRefreshLoadMoreListener {


    @BindView(R.id.mRecyclerView)
    RecyclerView mRecyclerView;
    @BindView(R.id.mSmartRefreshLayout)
    SmartRefreshLayout mSmartRefreshLayout;
    FreezoneAdapter freezoneAdapter;
    ClassicsFooter mClassicsFooter;
    int page = 0;
    FreezoneActivity freezoneActivity;

    public GirlFragment() {
    }

    public static GirlFragment girlFragment;

    public static GirlFragment newInstance() {
        if (null == girlFragment) {
            synchronized (GirlFragment.class) {
                if (null == girlFragment) {
                    girlFragment = new GirlFragment();
                }
            }
        }
        return girlFragment;
    }

    @Override
    protected void initPresenter() {
        mPresenter = new FragmentFreezonPresenter(this);
    }

    @Override
    protected void initView() {
        freezoneActivity = (FreezoneActivity) mActivity;
        freezoneAdapter = new FreezoneAdapter(R.layout.adapter_freezone, null);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        mRecyclerView.setLayoutManager(linearLayoutManager);
        mRecyclerView.setAdapter(freezoneAdapter);
        //自定义分割线
        DividerItemDecoration divider = new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL);
        divider.setDrawable(ContextCompat.getDrawable(getContext(), R.drawable.divider_line));
        mRecyclerView.addItemDecoration(divider);
        freezoneAdapter.openLoadAnimation(BaseQuickAdapter.SCALEIN);
        mSmartRefreshLayout.setOnRefreshLoadMoreListener(this);
        mClassicsFooter = (ClassicsFooter) mSmartRefreshLayout.getRefreshFooter();
    }

    @Override
    protected void retryRefreshData() {
        showProgress();
        page = 0;
        mPresenter.quearFreezonData(56, 2, page);
    }


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_girl;
    }

    /**
     * 加载跟多
     *
     * @param refreshLayout
     */
    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        page++;
        mPresenter.quearFreezonData(56, 2, page);
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        page = 0;
        mPresenter.quearFreezonData(56, 2, page);
    }


    @Override
    public void showFreezonData(List<FreezoneBean> list) {
        mSmartRefreshLayout.finishRefresh();
        if (page == 0) {
            freezoneAdapter.setNewData(list);
        } else {
            freezoneAdapter.addData(list);
        }
    }

    @Override
    public SmartRefreshLayout getmSmartRefreshLayout() {
        return mSmartRefreshLayout;
    }

    @Override
    public void noMoreData() {
        mClassicsFooter.setNoMoreData(true);
    }

    @Override
    public void initData() {
        page = 0;
        mPresenter.quearFreezonData(56, 2, page);
    }

    @Override
    public void showContent() {
        dissLoading();
    }

    @Override
    public void showProgress() {
        showLoading();
    }

    @Override
    public void showError(Throwable throwable) {
        ToastUtil.makeShortToast("网络异常");
        showNetErrorView(R.id.rlContent);
    }

    @Override
    public void showEmpty() {
        Toast.makeText(mActivity, "没数据", Toast.LENGTH_SHORT).show();
    }
}
